//
//  KYTextView.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/10/11.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYTextView.h"

@interface KYTextView()
/**
 *  提示文字
 */
@property (nonatomic, weak) UILabel *placeholderLabel;

/**
 *  字数限制
 */
@property (nonatomic, weak) UILabel *limitLabel;
@end

@implementation KYTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        // 0.设置自身属性
        self.layer.borderWidth = 1.0f;
        self.layer.borderColor = KYColor(230, 230, 230, 1.0).CGColor;
        
        // 1.添加提示文字
        UILabel *placeholderLabel = [[UILabel alloc] init];
        placeholderLabel.textColor = UIColorFromRGB(0xa2a9b2);
        placeholderLabel.hidden = YES;
        placeholderLabel.numberOfLines = 0;
        placeholderLabel.backgroundColor = [UIColor clearColor];
        placeholderLabel.font = [UIFont systemFontOfSize:15.5f];
        [self insertSubview:placeholderLabel atIndex:0];
        self.placeholderLabel = placeholderLabel;
        self.contentInset = UIEdgeInsetsMake(0, 2, 0, 0);
        
        // 2.文字限制
        UILabel *limitLabel = [[UILabel alloc] init];
        limitLabel.text = NSLocalizedString(@"0/35", nil);
        limitLabel.textColor = [UIColor lightGrayColor];
        limitLabel.font = [UIFont systemFontOfSize:15.0f];
        limitLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:limitLabel];
        self.limitLabel = limitLabel;
        
        // 3.监听textView文字改变的通知
        [KYNotificationCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat limitLabelW = 50;
    CGFloat limitLabelH = 30;
    CGFloat limitLabelX = self.frame.size.width - limitLabelW - 10;
    CGFloat limitLabelY = self.frame.size.height - limitLabelH;
    self.limitLabel.frame = CGRectMake(limitLabelX, limitLabelY, limitLabelW, limitLabelH);
}

- (void)setPlaceholder:(NSString *)placeholder
{
#pragma warning - NSString 赋值 一定要调用copy
    _placeholder = [placeholder copy];
    
    self.placeholderLabel.text = placeholder;
    if (placeholder.length) { // 需要显示
        self.placeholderLabel.hidden = NO;
        
        // 计算frame
        CGFloat placeholderX = 4;
        CGFloat placeholderY = 7;
        CGFloat maxW = self.frame.size.width;
        CGFloat maxH = self.frame.size.height - 2 * placeholderY;
        CGSize placeholderSize = [placeholder boundingRectWithSize:CGSizeMake(maxW, maxH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :self.placeholderLabel.font} context:nil].size;
        self.placeholderLabel.frame = CGRectMake(placeholderX, placeholderY, placeholderSize.width, placeholderSize.height);
    } else {
        self.placeholderLabel.hidden = YES;
    }
}

// 占位文本颜色
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    
    self.placeholderLabel.textColor = placeholderColor;
}

// 占位文本透明度
- (void)setPlaceholderLabelAlpha
{
    self.placeholderLabel.alpha = 0;
}

- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    
    self.placeholderLabel.font = [UIFont systemFontOfSize:13];
    self.placeholder = self.placeholder;
}

- (void)textDidChange
{
    self.placeholderLabel.hidden = (self.text.length != 0);
    self.placeholderLabel.alpha = 1.0;
    
    self.limitLabel.text = [NSString stringWithFormat:@"%ld/35",(unsigned long)self.text.length];
    
    if (self.text.length >= 35) {
        [MBProgressHUD showError:NSLocalizedString(@"描述超出限制...", nil)];
    }
}

- (void)dealloc
{
    [KYNotificationCenter removeObserver:self];
}
@end
